#include <iostream>
using std::cout;
using std::endl;

class Stack{
public:
    Stack(int max_len);
    bool empty();
    bool full();
    void push(int val);
    void pop();
    int top();
    ~Stack();
private:
    int _max_len;
    int _top_pos; //指向栈顶元素
    int *arr;
};

void test(){
    Stack s1(3);
    s1.empty();
    s1.push(1);
    s1.push(2);
    cout<<"top="<<s1.top()<<endl;
    s1.push(3);
    s1.push(4);
}

int main()
{
    

    test ();
    return 0;
}

Stack::Stack(int max_len)
:_max_len(max_len),_top_pos(-1),arr(new int[_max_len]()) 
{
    cout<<"stack init"<<endl;
}

bool Stack::full(){
    if(_top_pos == _max_len -1){
        cout<<"Stack is full!"<<endl;
        return true;
    }
    return false;
}

bool Stack::empty(){
    if(_top_pos == -1){
        cout << "Stack is empty!"<<endl;
        return true;
    }
    return false;
}

void Stack::push(int val){
    if(full() == false){
        _top_pos += 1;
        arr[_top_pos] = val;
    }
}

void Stack::pop(){
    if(empty() == false){
        cout << "pop val:"<<arr[_top_pos]<<endl;
        _top_pos -= 1;
    }
}

int Stack::top(){
    if(empty()== false){
        return arr[_top_pos];
    }
    return -1;
}

Stack::~Stack(){
    if(arr){
        delete [] arr;
        arr = nullptr;
    }
    cout << "~stack"<<endl;
}
